home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 9 / The PC-SIG Library on CD ROM - Ninth Edition.iso / 1501_600 / DISK1533 / DISK1533.ZIP / TP.C < prev    next >
Text File  |  1989-02-22  |  4KB  |  169 lines

  1. #include <stdio.h>
  2. #include <local.h>
  3.  
  4. static char *Program[] = { "tp.c - type files  01-16-88",
  5.                "S. Leoce, V1.0 R1.0 va@psj" };
  6.  
  7. static char *Usage = "usage: tp [/chnpHLn /Sn /Fn] file [file ... ]";
  8.  
  9. #define max(x, y)  ((x) > (y) ? (x) : (y))
  10.  
  11. #include "cmdline.c"
  12.  
  13. #define    MAXLINE    255
  14. #define NONE    0
  15. #define DEFAULT    24
  16. #define TOP    0
  17.  
  18. main(argc, argv)
  19. int argc;
  20. char *argv[];
  21. {
  22.     auto char bufr[BUFSIZ];
  23.     auto short count = FALSE, header = FALSE, number = FALSE;
  24.     auto short LineOption = FALSE, pause = FALSE;
  25.     register long Number = NONE;
  26.     auto int Lines = DEFAULT;    /* default lines  */
  27.     auto int space = NONE;  /* default space lines    */
  28.     auto int First = TOP;   /* top is default of file */
  29.     FILE *fp;
  30.     char line [MAXLINE];    /* maximum input line     */
  31.  
  32.     opterr = FALSE;
  33.     while ((LineOption = getopt(argc, argv, "chnpHF:L:S:")) != EOF)
  34.         switch(LineOption) {
  35.         case '?':
  36.             fprintf(stderr,"tp: illegal option %c\n",badopt);
  37.             fprintf(stderr,"tp: %s\n", Usage);
  38.             _exit(0x04);
  39.         case 'c':
  40.             count = TRUE;
  41.             break;
  42.         case 'h':
  43.             header = TRUE;
  44.             break;
  45.         case 'n':
  46.             number = TRUE;
  47.             break;
  48.         case 'p':
  49.             pause = TRUE;
  50.             break;
  51.         case 'S':
  52.             space = max(atoi(optarg), 0);
  53.             break;
  54.         case 'L':
  55.             if((Lines = max(atoi(optarg), 0)) == 0)
  56.                 _exit(fprintf(stderr,"tp: pause count must "
  57.                     "be greater zero\n") + 0x10);
  58.             break;
  59.         case 'F':
  60.             if((First = max(atoi(optarg), 0)) == 0)
  61.                 _exit(fprintf(stderr,"tp: first line must "
  62.                     "be greater zero\n") + 0x12);
  63.             break;
  64.         case 'H':
  65.             _exit(help() + 0x00);
  66.         }
  67.  
  68.     setbuf(stdout, bufr);    
  69.     if (argv[optind] == NULL) {    /* goto filter mode, 1 time only */
  70.         register int left = Lines, first = First;
  71.  
  72.         while (--first > 0 && fgets(line, MAXLINE, stdin) != NULL)
  73.             Number++;
  74.  
  75.         while (fgets(line, MAXLINE, stdin) != NULL) {
  76.             auto int i = EOF;
  77.  
  78.             Number++;
  79.             if (pause && --left == 0) {
  80.                 fflush(stdout);
  81.                 fprintf(stderr,"\r--More--");
  82.                 if (feof(stdin) || toupper(getch()) == 'Q')
  83.                     _exit(0x00);
  84.                 fprintf(stderr,"\r        \r");
  85.                 left = Lines;
  86.             }
  87.             if (number)
  88.                 printf("%ld: ", Number);
  89.             printf("%s", line);
  90.             for (i=1; i <= space; i++)
  91.                 putchar('\n');
  92.         }
  93.         if (count)
  94.             printf("\n\n%ld Lines, %d Typed\n",Number,Number-First);
  95.         fflush (stdout);
  96.         _exit (0x00);
  97.     }
  98.     else    /* read the files from the command line */
  99.     {
  100.         while(argv[optind] != NULL) {
  101.             if((fp = fopen(argv[optind++], "r")) == NULL) {
  102.                 fprintf(stderr,"tp: can't open %s",
  103.                     argv[optind-1]);
  104.                 perror(" - Reason");
  105.             }
  106.             else
  107.             {
  108.                 register int left = Lines;
  109.                 register int first = First;
  110.                 
  111.                 if(setvbuf(fp, NULL, _IOFBF, 4K) != 0)
  112.                     fprintf(stderr,"tp: low core - input"
  113.                         " unbuffered\n");
  114.                 Number = 0;
  115.                 if (header)
  116.                     printf("\n---------------- %s\n",
  117.                         argv[optind-1]);
  118.                 else
  119.                     putchar('\n');
  120.                 while(--first > 0 && fgets(line,MAXLINE,fp)
  121.                     != NULL)  Number++;
  122.  
  123.                 while(fgets(line,MAXLINE,fp) != NULL) {
  124.                     auto short i;
  125.  
  126.                     Number++;
  127.                     if (pause && --left == 0) {
  128.                         fflush(stdout);
  129.                         fprintf(stderr,"\r--More--");
  130.                         if (feof(stdin) || toupper(getch()) == 'Q')
  131.                             _exit(0x00);
  132.                         fprintf(stderr,"\r        \r");
  133.                         left = Lines;
  134.                     }
  135.                     if (number)
  136.                         printf("%ld: ",Number);
  137.                     printf("%s",line);
  138.                     for (i=1; i <= space; i++)
  139.                         putchar('\n');
  140.                 }
  141.                 if (First > Number)
  142.                     fprintf(stderr,"tp: first line specified "
  143.                         "larger than file length\n");
  144.                 fclose(fp);    /* close input file */
  145.                 if (count)
  146.                     printf("\n\n%ld Lines, %d Typed\n",Number,Number-First);
  147.                 fflush(stdout);    /* clean the buffer */
  148.             }
  149.         }
  150.     }
  151.     _exit (0x00);
  152. }
  153.  
  154. help()
  155. {
  156.     fprintf(stderr, "TP Help\n\nOptions:\n");
  157.     fprintf(stderr,"\t/c  - count total lines        ( default OFF )\n"
  158.                "\t/h  - header before output     ( default OFF )\n"
  159.                "\t/n  - number output lines      ( default OFF )\n"
  160.                "\t/p  - pause before scroll      ( default OFF )\n"
  161.                 "\t/H  - show options help        ( default OFF )\n"
  162.                "\t/Ln - use n lines per page     ( default 24  )\n"
  163.                "\t/Sn - space n lines in output  ( default 0   )\n"
  164.                "\t/Fn - first output line is n   ( default 0   )\n");
  165.     fprintf(stderr,"\n\tn an integer, greater than zero\n"
  166.                "\tq or Q at the --More-- prompt exits\n");
  167.  
  168.     return (0);
  169. }